/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.JLabel;
import javax.swing.border.Border;

/**
 * This class is for displaying the default labels in PearReview. Some 
 * important things like no HTML are presetted and the default Font in
 * the programm.
 *
 */
public class FormLabel extends JLabel {
	
	private static final long serialVersionUID = -2318658231277486787L;
	private boolean focus = false;

	public FormLabel(String text) {
		super();
		init();
		setText(text);
	}

	public FormLabel() {
		super();
		init();
	}
	
	private void init() {
		setFont(new Font("Tahoma", Font.PLAIN, 12));
		putClientProperty("html.disable", Boolean.TRUE);
		putClientProperty("html", null); 
	}
	
	/**
	 * Sets the visual focus to the label displayed by underlined text.
	 * Underlined text is used in other components of PearReview too for 
	 * displaying the focus to the user.
	 * 
	 * @param focus
	 */
	public void setFocus(boolean focus) {
		this.focus = focus;
		repaint();
	}
	
	@Override
	public void paint(Graphics g) {
		  Rectangle r;
		  super.paint(g);
		  
		  //underline the text if focused
		  if (focus) {
			  r = g.getClipBounds();
			  
			  //calculate the border offset
			  Border border = this.getBorder();
			  int offset = 0;
			  if (border != null && border.getBorderInsets(this) != null) {
				  Insets borderInstets = border.getBorderInsets(this);
				  offset = borderInstets.left;
			  }
			  
			  g.drawLine(offset,
					  r.height - this.getFontMetrics(
							  this.getFont()).getDescent()+1, 
					  this.getFontMetrics(this.getFont()).stringWidth(
							  this.getText())+offset,  
					  r.height - this.getFontMetrics(
							  this.getFont()).getDescent()+1);
		  }
	}

}
